#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    setFixedSize(size());
    for (const auto& name : role_name) {
        int sum {};
        for (const auto& rs : mayu[name])
            sum += rs.road().count();
        ui->roles->addItem(tr("%1线：%2条").arg(name).arg(sum));
    }
    ui->roles->addItem(tr("单身线：%1条").arg(mayu.failure().count()));
    ui->roles->setCurrentRow(role_name.indexOf(role_default));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::read_roads(const QList<Data::Roads>& roads)
{
    int sum {};
    now_roads.clear();
    for (const auto& rs : roads) {
        QString loser;
        if (not rs.names().empty())
            loser = tr("（击败：%1）").arg(rs.names().join(tr("、")));
        for (const auto& r : rs.road()) {
            now_roads.append(r);
            ui->roads->addItem(tr("路线%1%2").arg(++sum).arg(loser));
        }
    }
}

void MainWindow::on_roles_currentRowChanged(int current_row)
{
    ui->roads->clear();
    ui->road->clear();
    if (current_row < 0)
        return;
    else if (current_row >= role_name.count()) {
        now_roads = mayu.failure();
        for (int i{}; i!=now_roads.count(); )
            ui->roads->addItem(tr("路线%1").arg(++i));
    }
    else
        read_roads(mayu[role_name[current_row]]);
}

void MainWindow::on_roads_currentRowChanged(int current_row)
{
    if (current_row < 0)
        return;
    ui->road->clear();
    const Data::Road& road {now_roads[current_row]};
    for (const auto& it : road.get_options())
        ui->road->addItem(it);
    ui->road->addItem("");
    for (const auto& name : role_name)
        ui->road->addItem(tr("%1：%2/%3").arg(name).arg(road.favors()[name]).arg(mayu.need(name)));
}
